package com.example.charactivity.ChartWeekly

import android.app.usage.UsageStats
import android.app.usage.UsageStatsManager
import android.content.Context
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.example.charactivity.ChartDaily.DayChartViewModel
import com.example.charactivity.GernerChartModel
import com.example.charactivity.UsageStatics
import com.example.recyclerview.UsingData
import com.github.aachartmodel.aainfographics.aachartcreator.AAChartType
import com.github.aachartmodel.aainfographics.aachartcreator.AASeriesElement
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.*
import kotlin.concurrent.thread

class WeeklyViewModel : ViewModel() {


    var changeMaker=MutableLiveData<Long>()
    lateinit var currntTime: Calendar
    companion object weekly{
        var arrayData= arrayOfNulls<Long>(8)
        var arrayPackName= arrayOfNulls<String>(8)
        var appinfo= arrayListOf<UsingData>()

        fun configureChart() {

            /*仅仅更新了图表的series数组数据,不改动图表的其他内容*/
            val n1: Long = arrayData[0]!!
            val n2:Long=arrayData[1]!!
            val n3:Long= arrayData[2]!!
            val n4:Long = arrayData[3]!!
            val n5:Long = arrayData[4]!!
            val n6:Long =arrayData[5]!!
            val n7:Long =arrayData[6]!!
            val n8:Long =arrayData[7]!!
            GernerChartModel.aaChartModel.series(
                arrayOf(
                    AASeriesElement()
                        .showInLegend(false)
                        .name("the week")
                        .color("#f48fb1")
                        .type(AAChartType.Pie)
                        .data(arrayOf(n1,n2,n3,n4,n5,n6,n7,n8))
                )

            )
                .dataLabelsEnabled(false)
                .subtitle("this should be the week")
        }
    }

    fun sortData(usageStatsList: MutableList<UsageStats>) :Long{


        appinfo.clear()
        usageStatsList.sortBy {
            it.totalTimeInForeground
        } // 排序
        usageStatsList.reverse()
        val top12:List<UsageStats>
        if(usageStatsList.size>=12)  // 获取前6个！
            top12=usageStatsList.subList(0,12)
        else
            top12=usageStatsList
        for(its in top12){

            // 获取前12个最常用的
            val h=its.totalTimeInForeground/60/60/1000
            // 得到小时的数目
            var min:Int
            if(h>0){
                min= ((its.totalTimeInForeground/60/1000)%h).toInt()
            }
            else
                min= (its.totalTimeInForeground/60/1000).toInt()

            val str2=its.packageName.replace("com.","")
            // 删除某个字符串
            val temp=UsingData(str2,"${h}h${min}min")
            appinfo.add(temp)
        }
        val moreUsed:List<UsageStats>
        if(usageStatsList.size>=8)  // 获取前6个！
            moreUsed=usageStatsList.subList(0,8)
        else
            moreUsed=usageStatsList
// 完成排序
        var i=0
        Log.i("bpt","size:${moreUsed.size}")
        for (usageStats in moreUsed) {

            WeeklyViewModel.arrayData[i]= usageStats.totalTimeInForeground
            Log.d("search", arrayData[i].toString())
            WeeklyViewModel.arrayPackName[i]=usageStats.packageName
            i++   // 气死你爹了，忘记循环了！！！！！！！！！
            //  usageStatics. printState(usageStats)
        }
        return  arrayData[i-1]!!


    }


}